# Maintainer: Diego Sogari <diego.sogari@gmail.com>
# Contributor: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=binaryen
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=99
pkgrel=1
pkgdesc="Compiler infrastructure and toolchain library for WebAssembly, in C++ (mingw-w64)"
arch=('any')
url="https://github.com/WebAssembly/binaryen"
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/WebAssembly/binaryen/archive/version_${pkgver}.tar.gz")
sha256sums=('66ac4430367f2096466703b81749db836d8f4766e542b042d64e78b601372cf7')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    "../${_realname}-version_${pkgver}"

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}-version_${pkgver}/LICENSE ${okgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
